package com.alks.common.aspect;

import com.alks.common.aop.Entity;
import com.alks.common.data.thread.UserIdThread;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author MS
 * @since 2024/8/24下午3:24
 */


public class EntityAspect {
    public static void initialize(Object obj) throws IllegalAccessException {
        /*定义创建时间的字段名*/
        final String sysDateFile = "sysDate";
        final String sysUserFile = "sysUserFile";
        final String companyFile = "companyId";

        Class<?> clazz = obj.getClass();
        if (clazz.isAnnotationPresent(Entity.class)) {
            Entity entity = clazz.getAnnotation(Entity.class);
            boolean sysDate = entity.sysDate();
            boolean num = entity.num();
            boolean date = entity.date();
            boolean company = entity.company();
            boolean sysUser = entity.sysUser();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                /*创建时间判定*/
                if (sysDate) {
                    boolean sysType = field.getName().equals(sysDateFile) && (field.getType() == LocalDateTime.class || field.getType() == LocalDate.class);
                    if (sysType) {
                        if (field.getType() == LocalDateTime.class) {
                            field.set(obj, LocalDateTime.now().toLocalDate().atStartOfDay());
                        } else {
                            field.set(obj, LocalDate.now());
                        }
                    }
                }

                /*时间初始化*/
                if (date) {
                    boolean dateType = field.getType() == LocalDateTime.class || field.getType() == LocalDate.class;
                    if (dateType) {
                        if (field.getType() == LocalDateTime.class) {
                            field.set(obj, LocalDateTime.now().toLocalDate().atStartOfDay());
                        } else {
                            field.set(obj, LocalDate.now());
                        }
                    }
                }

                /*数字初始化*/
                if (num) {
                    boolean numType = field.getType() == BigDecimal.class ||
                            field.getType() == Integer.class ||
                            field.getType() == Double.class ||
                            field.getType() == Float.class ||
                            field.getType() == Long.class ||
                            field.getType() == Short.class ||
                            field.getType() == Byte.class;
                    if (numType) {
                        if (field.getType() == BigDecimal.class) {
                            field.set(obj, BigDecimal.ZERO);
                        } else if (field.getType() == Integer.class) {
                            field.set(obj, 0);
                        } else if (field.getType() == Double.class) {
                            field.set(obj, 0.0);
                        } else if (field.getType() == Float.class) {
                            field.set(obj, 0.0f);
                        } else if (field.getType() == Long.class) {
                            field.set(obj, 0L);
                        } else if (field.getType() == Short.class) {
                            field.set(obj, (short) 0);
                        } else {
                            field.set(obj, (byte) 0);
                        }
                    }
                }

                /*公司初始化*/
                if (company) {
                    boolean companyType = field.getName().equals(companyFile) || field.getType() == String.class;
                    if (companyType) {
                        field.set(obj, UserIdThread.get().getCompanyId());
                    }
                }

                /*创建人初始化*/
                if (sysUser) {
                    boolean userType = field.getName().equals(sysUserFile) || field.getType() == String.class;
                    if (userType) {
                        field.set(obj, UserIdThread.get().getUserId());
                    }
                }

            }
        }
    }
}
